package com.spring.mybatis;

import com.my.mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author zhupanlin
 * @version 1.0
 * @description: TODO
 * @date 2024/6/16 13:56
 */
public class MyFactoryBean implements FactoryBean {
    
    private SqlSession sqlSession;
    
    private Class mapperClass;

    public MyFactoryBean(Class mapperClass) {
        this.mapperClass = mapperClass;
    }

    @Autowired 
    public void setSqlSession(SqlSessionFactory sqlSessionFactory) {
        sqlSessionFactory.getConfiguration().addMapper(mapperClass);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    @Override
    public Object getObject() throws Exception {
        
        return sqlSession.getMapper(mapperClass);
    }

    @Override
    public Class<?> getObjectType() {
        return mapperClass;
    }
}
